package com.wu.mvp.app.base.list;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.wu.mvp.R;
import com.wu.mvp.app.base.BaseActivity;
import com.wu.mvp.utils.StatusBarUtil;
import com.wu.mvp.widget.recyclerview.EasyRecyclerView;
import com.wu.mvp.widget.recyclerview.adapter.BaseViewHolder;

public abstract class BaseListActivity<P extends BaseListPresenter, M> extends BaseActivity<P> {

    Context mCtx;
    private EasyRecyclerView recycler;
    //recycler是否可下拉刷新
    boolean isRefresh = true;
    //recycler是否可上拉加载
    boolean isLoadMore = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCtx = this;
        if (getLayout() == 0) {
            setContentView(R.layout.activity_base);
        } else {
            setContentView(getLayout());
        }
        initView();
    }

    /**
     * 初始化状态栏和recyclerView
     */
    public void initView() {
        //沉浸式状态栏
        StatusBarUtil.immersive(this);
        View content = findViewById(R.id.content);
        if (content != null) {
            //设置与状态栏的间距
            StatusBarUtil.setPaddingSmart(this, content);
        }
        View view = findViewById(R.id.recycler);
        if (view == null) {
            throw new RuntimeException("No found recycler with id \"recycler\"");
        }
        if (view instanceof EasyRecyclerView) {
            recycler = (EasyRecyclerView) view;
            recycler.setLayoutManager(new LinearLayoutManager(this));
            recycler.setRefreshEnable(true);
            recycler.setLoadMoreEnable(true);
            recycler.setAdapter(getPresenter().getAdapter());

        } else {
            throw new RuntimeException("The type of recycler is not EasyRecyclerView");
        }
    }

    /**
     * 获取布局，重载可设置新布局，默认R.layout.activity_base
     * @return
     */
    public int getLayout() {
        return 0;
    }

    public EasyRecyclerView getRecycler() {
        return recycler;
    }

    /**
     * recyclerView的item的ViewHolder
     * @return
     */
    public abstract BaseViewHolder<M> getViewHolder(ViewGroup parent, int viewType);

    /**
     * 单个item的类型，用于多布局
     * @param position
     * @return
     */
    public int getViewType(int position) {
        return 0;
    }

    /**
     * 停止刷新
     * @param success 刷新成功或失败
     */
    public void stopRefresh(boolean success) {
        recycler.finishRefresh(success);
    }

    /**
     * 停止加载
     * @param success 加载成功或失败
     */
    public void stopLoadMore(boolean success) {
        recycler.finishLoadMore(success);
    }

    /**
     * 当前页是否允许下拉刷新
     * @param enable
     */
    public void setRefreshEnable(boolean enable) {
        isRefresh = enable;
        recycler.setRefreshEnable(enable);
    }

    /**
     * 当前页是否允许上拉加载
     * @param enable
     */
    public void setLoadMoreEnable(boolean enable) {
        isLoadMore = enable;
        recycler.setLoadMoreEnable(enable);
    }

    /**
     * 展示错误页
     */
    public void showError() {
        recycler.showError();
    }

    /**
     * 展示内容为空页
     */
    public void showEmpty() {
        recycler.showEmpty();
    }

}
